﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;
using Microsoft.Extensions.Logging;
using Abp.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace Abp.DbMigrator;

public class Program
{

    static async Task Main(string[] args) {
        Log.Logger = new LoggerConfiguration()
          .MinimumLevel.Information()
          .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
          .MinimumLevel.Override("Volo.Abp", LogEventLevel.Warning)
          .MinimumLevel.Override("Acme.BookStore", LogEventLevel.Debug)
          .MinimumLevel.Override("Acme.BookStore", LogEventLevel.Information)
          .Enrich.FromLogContext()
          .WriteTo.Async(c => c.File("Logs/logs.txt"))
          .WriteTo.Async(c => c.Console())
          .CreateLogger();
        await CreateHostBuilder(args).RunConsoleAsync();
    }


    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .AddAppSettingsSecretsJson()
            .ConfigureLogging((context, logging) => logging.ClearProviders())
            .ConfigureServices((hostContext, services) =>
            {
                var configuration = hostContext.Configuration;
                var connectionString = configuration.GetConnectionString("Default");

                // 添加MSSQL数据库连接
                services.AddDbContext<AbpDbContext>(options => options.UseSqlServer(connectionString,
                    action => action.MigrationsAssembly("Abp.DbMigrator")));

                // 添加数据库迁移服务
                services.AddHostedService<DbHostService>();
            });
}
